<?
/*
%%%copyright%%%
 * phpMyTicket - ticket reservation system
 * Copyright (C) 2004-2005 Anna Putrino, Stanislav Chachkov. All rights reserved.
 *
 * This file is part of phpMyTicket.
 *
 * This file may be distributed and/or modified under the terms of the
 * "GNU General Public License" version 2 as published by the Free
 * Software Foundation and appearing in the file LICENSE included in
 * the packaging of this file.
 *
 * Licencees holding a valid "phpmyticket professional licence" version 1
 * may use this file in accordance with the "phpmyticket professional licence"
 * version 1 Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 *
 * The "phpmyticket professional licence" version 1 is available at
 * http://www.phpmyticket.com/ and in the file
 * PROFESSIONAL_LICENCE included in the packaging of this file.
 * For pricing of this licence please contact us via e-mail to 
 * info@phpmyticket.com.
 * Further contact information is available at http://www.phpmyticket.com/
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Contact info@phpmyticket.com if any conditions of this licencing isn't 
 * clear to you.
 
 */
?>
<?
require_once("admin/EventViewCommon.php");
require_once("classes/ShopDB.php");
require_once("classes/Event.php");
require_once("functions/datetime_func.php");

class EventSubPropsView extends EventViewCommon {

function event_view ($event_id){
  global $_SHOP;

  $query="select * FROM (Event,Ort) LEFT JOIN PlaceMap2 ON Event.event_pm_id=PlaceMap2.pm_id where event_id=$event_id and event_ort_id=ort_id and event_organizer_id='{$_SHOP->organizer_id}'";
  if(!$data=ShopDB::query_one_row($query)){return;}

  if(!$main=Event::load($data['event_main_id'],FALSE) or $main->event_organizer_id!=$_SHOP->organizer_id){return FALSE;}

  $data_f["event_date"]=$data["event_date"];
  $data_f["event_time"]=$data["event_time"];
  $data_f["event_open"]=$data["event_open"];
  $data["event_date"]=formatAdminDate($data["event_date"]);
  $data["event_time"]=formatTime($data["event_time"]);
  $data["event_open"]=formatTime($data["event_open"]);


  $event_id=$data["event_id"];

  if($data["event_status"]=='unpub'){
    $title="Publish";
    $alt="Publish";
    $src="images/publish.jpg";
    $link="publish_event.php?event_id=$event_id";
  }else if ($data["event_status"]=='pub'){
    $title="Unpublish";
    $alt="Unpublish";
    $src="images/unpublish.jpg";
    $link="unpublish_event.php?event_id=$event_id";
  }else if ($data["event_status"]=='nosal'){
    $title="Archive";
    $alt="Archive";
    $src="images/archive.jpg";
    $link="archive_event.php?event_id=$event_id";
    $title1="Publish";
    $alt1="Publish";
    $src1="images/publish.jpg";
    $link1="republish_event.php?event_id=$event_id";
    $title2="Delete";
    $alt2="Delete";
    $src2="images/trash.png";
    $link2="delete_event.php?event_id=$event_id";
    
  }
  
  echo "<table class='admin_form' width='500' cellspacing='1' cellpadding='4'>\n";
  echo "<tr><td colspan='2'><table border='0' width='100%' cellpadding='0' cellspacing='0'><tr><td class='admin_list_title'>".$data["event_name"]."
        </td><td align='right' width='150'>";
   if($link1){
    echo "<a href='$link1'><img border='0' alt='$alt1' title='$title1' src='$src1'></a> ";   
   }
   	
   echo "<a href='$link'><img border='0' alt='$alt' title='$title' src='$src'></a> ";
   if($link2){
    echo "<a href='$link2'><img border='0' alt='$alt2' title='$title2' src='$src2'></a> ";   
   }	

   echo "</td></tr></table></td></tr>";   
    
  $this->print_field('event_id',$data );
  $this->print_field('event_date',$data , $this->_chk('event_date',$data_f,$main)  );
  $this->print_field('event_name',$data, $this->_chk('event_name',$data,$main) );
  $this->print_field('ort_name',$data );
  $this->print_field('event_short_text',$data, $this->_chk('event_short_text',$data,$main)  );
  $this->print_field('event_text',$data, $this->_chk('event_text',$data,$main)  );
 // $this->print_url('event_image',$data );
  $this->print_url('event_url',$data, $this->_chk('event_url',$data,$main)  ); 
  $this->print_field('event_time',$data, $this->_chk('event_time',$data_f,$main)  );
  $this->print_field('event_open',$data, $this->_chk('event_open',$data_f,$main)  );
  $this->print_field('event_group_name',$data);
  
  $this->print_field_o('event_order_limit',$data , $this->_chk('event_order_limit',$data,$main) );
  //cc or post ---> in init
//  $this->print_field('event_payment',$data );
  $this->print_field('event_template',$data, $this->_chk('event_template',$data,$main) );
  $this->print_type('event_type',$data);

  echo "<tr><td class='admin_name'>".event_discounts."</td>
  <td class='admin_value'>
  <a class='link' href='view_discount.php?discount_event_id={$data['event_id']}'>".view."</a>
  </td></tr>\n";  
  
  
  $this->view_file('event_image' ,$data,$err,'img',$this->_chk('event_image',$data,$main));
  $this->view_file('event_mp3' ,$data,$err,'mp3',$this->_chk('event_mp3',$data,$main));
  
//  if($data['event_image']){
//   echo "<tr><td class='admin_name'  width='40%'>".$this->con('event_image'). $this->_chk('event_image',$data,$main) ."</td>
//        <td class='admin_value'><img src='".$data['event_image']."'></td></tr>";
//   }

//  if($data['event_ort_image']){
//   echo "<tr><td class='admin_name'  width='40%'>".$this->con('event_ort_image'). $this->_chk('event_ort_image',$data,$main). "</td>
//        <td class='admin_value'><img src='$dir/".$data['event_ort_image']."'></td></tr>";
//  }

//  if($data['event_mp3']){
//   echo "<tr><td class='admin_name'  width='40%'>".$this->con('event_mp3'). $this->_chk('event_mp3',$data,$main) ."</td>
//        <td class='admin_value'><a href='{$data['event_mp3']}'>".event_audio."</a></td></tr>";
//   }
    
  echo "</table>\n";
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?event_main_id={$data['event_main_id']}'>".admin_list."</a></center>";

}


function new_event_form (&$data, &$err, $title){
  global $_SHOP;

/*  echo "
  <script><!--
    function getElement(id){
      if(document.all) {return document.all(id);}
      if(document.getElementById) {return document.getElementById(id);}
    }
  
    function ext(chk){
      exts=getElement('ext');
      if(!ext){return;}
      if(chk.checked){
        exts.style.display='block';
      }else{
        exts.style.display='none';
      }    
    }
  --></script>
  ";
*/
  
  echo "<form method='POST' action='{$_SERVER['PHP_SELF']}' enctype='multipart/form-data'>\n";

  if(!$main=Event::load($data['event_main_id'],FALSE) or $main->event_organizer_id!=$_SHOP->organizer_id){return FALSE;}

  $this->form_head($title);
    
  $this->print_date('event_date',$data,$err);
  $this->print_select_pm('event_pm_ort_id',$data,$err,($main->event_pm_id?'has_def':''));
  $this->print_time('event_time',$data,$err);
  $this->print_time('event_open',$data,$err);

/*
  echo "<tr><td class='admin_name'  width='40%'>".extended."</td>
        <td class='admin_value'>
        <input type='checkbox' name='extended' value='1' onChange='ext(this);'>
        </td></tr>";
	
  echo "<tr><td colspan=2><table border=0 id='ext' width='100%' class='admin_form' style='display:none;'cellspacing='1' cellpadding='4'>\n";
    
  $this->print_input('event_name',$data,$err,30,100);

//  $this->print_select_ort('event_ort_id',$data,$err);

  $this->print_area('event_short_text',$data,$err,3);
  $this->print_area('event_text',$data,$err);
  $this->print_input('event_url',$data,$err,30,100);
  
  $this->print_input('event_order_limit',$data,$err,3,4);
  $this->print_select_tpl('event_template',$data,$err);
  
  $this->print_file('event_image',$data,$err,'img');  	
  $this->print_file('event_ort_image',$data,$err,'img');  	
  $this->print_file('event_mp3',$data,$err,'mp3');  	
  
  echo "</table></td></tr>";
*/  
  $this->form_foot();

  echo "<input type='hidden' name='action' value='insert'/>\n";
  echo "<input type='hidden' name='event_main_id' value='{$data['event_main_id']}'/>\n";
  
  
  echo "</form>\n";
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?event_main_id={$data['event_main_id']}'>".admin_list."</a></center>";
}

function update_event_form (&$data, &$err, $title){
  global $_SHOP;


  if(!$main=Event::load($data['event_main_id'],FALSE) or $main->event_organizer_id!=$_SHOP->organizer_id){return FALSE;}
  

/*
  echo "
  <script><!--
    function getElement(id){
      if(document.all) {return document.all(id);}
      if(document.getElementById) {return document.getElementById(id);}
    }
  
    function ext(chk){
      exts=getElement('ext');
      if(!ext){return;}
      if(chk.checked){
        exts.style.display='block';
      }else{
        exts.style.display='none';
      }    
    }
  --></script>
  ";
*/  
  echo "<form method='POST' action='{$_SERVER['PHP_SELF']}' enctype='multipart/form-data'>\n";

  $this->form_head($title);
  
    
  $this->print_date('event_date',$data,$err);

  $check=$this->_check('event_time',$main,$data);
  $this->print_time('event_time',$data,$err,$check);
  
  $check=$this->_check('event_open',$main,$data);
  $this->print_time('event_open',$data,$err,$check);

/*
  if($data['event_extended']){
    $chk='checked';
  }  

  echo "<tr><td class='admin_name'  width='40%'>".extended."</td>
        <td class='admin_value'>
        <input type='checkbox' $chk name='extended' value='1' onChange='ext(this);'>
        </td></tr>";
	
  echo "<tr><td colspan=2><table border=0 id='ext' width='100%' class='admin_form' style='display:none;'cellspacing='1' cellpadding='4'>\n";
*/
  $check=$this->_check('event_name',$main,$data);
  $this->print_input('event_name',$data,$err,30,100,$check);

//  $this->print_select_ort('event_ort_id',$data,$err);

  $check=$this->_check('event_short_text',$main,$data);
  $this->print_area('event_short_text',$data,$err,3,40,$check);

  $check=$this->_check('event_text',$main,$data);
  $this->print_area('event_text',$data,$err,6,40,$check);

  $check=$this->_check('event_url',$main,$data);
  $this->print_input('event_url',$data,$err,30,100,$check);
  
  $check=$this->_check('event_order_limit',$main,$data);
  $this->print_input('event_order_limit',$data,$err,30,100,$check);

  $check=$this->_check('event_template',$main,$data);
  $this->print_select_tpl('event_template',$data,$err,$check);
  
  $check=$this->_check('event_image',$main,$data);
  $this->print_file('event_image',$data,$err,'img',$check);  	

//  $check=$this->_check('event_ort_image',$main,$data);
//  $this->print_file('event_ort_image',$data,$err,'img',$check);  	

  $check=$this->_check('event_mp3',$main,$data);
  $this->print_file('event_mp3',$data,$err,'mp3',$check);  	
  
//  echo "</table></td></tr>";
  
  $this->form_foot();

  echo "<input type='hidden' name='action' value='update'/>\n";
  echo "<input type='hidden' name='event_id' value='{$data['event_id']}'/>\n";
  echo "<input type='hidden' name='event_main_id' value='{$data['event_main_id']}'/>\n";
  
  echo "</form>\n";
  echo "<br><center><a class='link' href='view_event_sub.php?event_main_id={$data['event_main_id']}'>".admin_list."</a></center>";
}



function event_list ($event_main_id){
  global $_SHOP;

  if(!$main=Event::load($event_main_id,FALSE) or $main->event_organizer_id!=$_SHOP->organizer_id){return;}

  $query="select * from Event,Ort where event_ort_id=ort_id and event_main_id='$event_main_id' and event_rep='sub' and event_organizer_id='{$_SHOP->organizer_id}' and event_status!='trash' order by event_date ";

  if(!$res=ShopDB::query($query)){
    return;
  }
  
  $alt=0;
  $this->list_head("{$main->event_name}",8);
      
  $img_pub['pub']='images/grun.png';
  $img_pub['unpub']='images/rot.png';
  $img_pub['nosal']='images/grey.png';
  
  $alt=0;
  
  while($row=mysql_fetch_assoc($res)){
  
    $edate=formatAdminDate($row["event_date"]);
    $etime=formatTime($row["event_time"]);

     echo "<tr class='admin_list_row_$alt'> 
            <td class='admin_list_item'><img src='{$img_pub[$row['event_status']]}'></td>
            <td class='admin_list_item'>{$row['event_id']}</td>
            <td class='admin_list_item'>$edate $etime</td>
            <td class='admin_list_item'>{$row['ort_name']}</td>";

     
     
     echo "<td class='admin_list_item'><a class='link' href='view_event_sub.php?action=view&event_id={$row['event_id']}'><img src='images/view.png' border='0' alt='".view."' title='".view."'></a></td> 
           <td class='admin_list_item'><a class='link' href='view_event_sub.php?action=edit&event_id={$row['event_id']}'><img src='images/edit.gif' border='0' alt='".edit."' title='".edit."'></a></td>\n";

     if($row['event_pm_id']){
       echo "<td class='admin_list_item'><a class='link' href='view_pm2.php?action=view_pm&pm_id={$row['event_pm_id']}'><img src='images/pm.png' border='0' alt='".place_map."' title='".place_map."'></a></td>\n";
     }else{
       echo "<td class='admin_list_item'>&nbsp;</td>";
     }

     if(($row['event_pm_id'] and $row['event_status']=='unpub') or (!$row['event_pm_id'] and $row['event_status']!='pub')){
       echo "<td class='admin_list_item'><a class='link' href='javascript:if(confirm(\"".delete_item."\")){location.href=\"view_event_sub.php?action=remove&event_id={$row['event_id']}\";}'><img src='images/trash.png' border='0' alt='".remove."' title='".remove."'></a></td>";
     }else{
       echo "<td class='admin_list_item'>&nbsp;</td>";
     }
     
     $alt=($alt+1)%2;
  }  
  
  echo "<tr><td class='admin_list_item' colspan=8 align=center><a class='link' href='view_event_sub.php?action=add&event_main_id=$event_main_id'>".add."</a></td></tr>
  
  </table>\n";
  echo "<br><center><a class='link' href='view_event.php'>".admin_list."</a></center>";
}



########################################################
########################################################
########################################################
function draw (){
  global $_SHOP;
  
  if($_POST['action']=='insert'){
    if(!$this->event_check($_POST,$err)){
      $this->add_event($_POST['event_main_id'],$_POST);
      $this->new_event_form($_POST,$err,event_add_title);
    }else{
      $this->insert_event($_POST);
      $this->event_list($_POST['event_main_id']);
    }  
  }else
  
  if($_POST['action']=='update'){
    if(!$this->event_check($_POST,$err)){
      $this->update_event_form($_POST,$err,event_update_title);
    }else{
      $this->update_event($_POST);
      $this->event_list($_POST['event_main_id']);
    }  
  }else

  if($_GET['action']=='add' and $_GET['event_main_id']){
    $this->add_event($_GET['event_main_id'],$_GET,$err);
  }
  
  elseif($_GET['action']=='view' and $_GET['event_id']){
    $this->event_view($_GET['event_id']);
  }

  elseif($_GET['action']=='edit' and $_GET['event_id']){
    $this->event_edit($_GET['event_id']);
  }
  
  elseif($_GET['action']=='remove' and $_GET['event_id']){
    if($event=Event::load($_GET['event_id'],FALSE)){
      if($_SHOP->organizer_id!=$event->event_organizer_id){return;} 
      
      $event->delete();
      $this->event_list($event->event_main_id);
    }
  }

  elseif($_GET['event_main_id']){
    $this->event_list($_GET['event_main_id']);
  }

}
########################################################
########################################################
########################################################


function event_check (&$data, &$err){
  global $_SHOP;
  
  
  if(!$main=Event::load($data['event_main_id'],FALSE) or $main->event_organizer_id!=$_SHOP->organizer_id){return FALSE;}
   
  
  if((isset($data['event_time-h']) and strlen($data['event_time-h'])>0) or 
     (isset($data['event_time-m']) and strlen($data['event_time-m'])>0))
  {
    $h=$data['event_time-h'];
    $m=$data['event_time-m'];
    if(!is_numeric($h) or $h<0 or $h>23){$err['event_time']=invalid;}
    else if(!is_numeric($m) or $h<0 or $m>59){$err['event_time']=invalid;}
    else{$data['event_time']="$h:$m";}
  }
  if(!$main->event_time and  !$data['event_time']){$err['event_time']=mandatory;}

  if($data['action']=='insert'){
    if($data['event_pm_ort_id']=='copy_main_pm' and $main->event_pm_id){
      $data['event_pm_id']=$main->event_pm_id;
      $data['event_ort_id']=$main->event_ort_id;
    }elseif($data['event_pm_ort_id']){
      list($event_pm_id,$event_ort_id)=explode(',',$data ['event_pm_ort_id']); 
      $data['event_pm_id']=$event_pm_id;
      $data['event_ort_id']=$event_ort_id;
    }else{
      $err['event_pm_ort_id']=mandatory;
    }
  }  

  if((isset($data['event_open-h']) and strlen($data['event_open-h'])>0) or 
    (isset($data['event_open-m']) and strlen($data['event_open-m'])>0))
  {
    $h=$data['event_open-h'];
    $m=$data['event_open-m'];
    if(!is_numeric($h) or $h<0 or $h>23){$err['event_open']=invalid;}
    else if(!is_numeric($m) or $h<0 or $m>59){$err['event_open']=invalid;}
    else{$data['event_open']="$h:$m";}
  }
  //if(!$main->event_open and  !$data['event_open']){$err['event_open']=mandatory;}
  
  if((isset($data['event_date-y']) and strlen($data['event_date-y'])>0) or 
     (isset($data['event_date-m']) and strlen($data['event_date-m'])>0) or 
     (isset($data['event_date-d']) and strlen($data['event_date-d'])>0))
  {
    $y=$data['event_date-y'];
    $m=$data['event_date-m'];
    $d=$data['event_date-d'];

    if(!checkdate($m,$d,$y)){$err['event_date']=invalid;}
    else{$data['event_date']="$y-$m-$d";}
  }else{
    $err['event_date']=mandatory;
  }
  if(!$main->event_date and  !$data['event_date']){$err['event_date']=mandatory;}

//print_r($err);
  return empty($err);
}



########################################################


  function insert_event (&$data){
    global $_SHOP;
   
    require_once('classes/Event.php');

    $event=Event::new_from_main($data['event_main_id'], FALSE);
    if(empty($event) or $event->event_organizer_id !=$_SHOP->organizer_id){echo a;return;}

    $event->event_date=$data['event_date'];
    if($data['event_time']){$event->event_time=$data['event_time'];}
    if($data['event_open']){$event->event_open=$data['event_open'];}
    $event->event_pm_id=$data['event_pm_id'];
    $event->event_ort_id=$data['event_ort_id'];
    
    if(!$event_id=$event->save()){return;}
        
    if(!$this->photo_post($_POST,$event_id)){
      echo "<div class=error>".img_loading_problem."<div>";
    }
    
    if(!$this->photo_post_ort($_POST,$event_id)){
      echo "<div class=error>".img_loading_problem."<div>";
    }

    if(!$this->mp3_post($_POST,$event_id)){
      echo "<div class=error>".mp3_loading_problem."<div>";
    }
  }

  function event_edit ($event_id){
    global $_SHOP;
  
    $query="SELECT * FROM Event LEFT JOIN Event_group ON Event.event_group_id=Event_group.event_group_id, Ort 
    WHERE ort_id=event_ort_id and event_id='$event_id' and event_organizer_id='{$_SHOP->organizer_id}'";
    if(!$row=ShopDB::query_one_row($query)){
      return;
    }
  
    $main=Event::load($row['event_main_id'],FALSE);

    if(!isset($main) or $main->event_organizer_id!=$_SHOP->organizer_id){
      return;
    }


    $this->update_event_form($row,$err,event_update_title."<br>({$main->event_id}) {$main->event_name} - {$main->ort_name}");
  }
  
  function update_event (&$data){
    global $_SHOP;

    $main=Event::load($data['event_main_id'], FALSE);
    if(empty($main) or $main->event_organizer_id !=$_SHOP->organizer_id){return;}

    $event=Event::load($data['event_id'], FALSE);
    if(empty($event) or $event->event_organizer_id !=$_SHOP->organizer_id){return;}

    $event->event_date=$data['event_date'];
    $this->_set('event_short_text',$data,$event,$main);
    $this->_set('event_text',$data,$event,$main);
    $this->_set('event_name',$data,$event,$main);
    $this->_set('event_url',$data,$event,$main);
    $this->_set('event_open',$data,$event,$main);
    $this->_set('event_order_limit',$data,$event,$main);
    $this->_set('event_template',$data,$event,$main);
    $this->_set('event_time',$data,$event,$main);
    $this->_set('event_image',$data,$event,$main);
//    $this->_set('event_ort_image',$data,$event,$main);
    $this->_set('event_mp3',$data,$event,$main);

    if($event->save()){return FALSE;}

    if(!$this->photo_post($data,$data['event_id'])){
      echo "<div class=error>".img_loading_problem."<div>";
    }
    
    if(!$this->photo_post_ort($data,$data['event_id'])){
      echo "<div class=error>".img_loading_problem."<div>";
    }
    
    if(!$this->mp3_post($data,$data['event_id'])){
      echo "<div class=error>".mp3_loading_problem."<div>";
    }  
  }  

  function add_event ($main_event_id,$data,$err){
    global $_SHOP;
    
    $main=Event::load($main_event_id,FALSE);


    if(!isset($main) or $main->event_organizer_id!=$_SHOP->organizer_id){
      return;
    }
    
    $this->new_event_form($data,$err,event_add_title."<br>({$main->event_id}) {$main->event_name} - {$main->ort_name}");
  }

  function _chk ($name,$data,$main){
    if($main->$name!=$data[$name]){
      return "<img src='images/grun.png'>";
    }  
  }

  function _check ($name,$main,$data){
    if($main->$name!=$data[$name]){$chk='checked';}
    return "<input type=checkbox name=$name"."_chk value=1 $chk align=middle style='border:0px;'> ";
  }

  function _set ($name,$data,&$event,$main){
    if($data[$name.'_chk']){
      $event->$name=$data[$name];
    }else{
      $event->$name=$main->$name;
    }
  }
}
?>
